package com.blood.modules.system.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blood.core.config.mybatisplus.ICommonServiceImpl;
import com.blood.core.resp.GlobalResultStatus;
import com.blood.core.resp.PageResult;
import com.blood.core.resp.ResponseResult;
import com.blood.core.util.ObjectAssertUtil;
import com.blood.core.util.bean.BeanUtil;
import com.blood.core.util.bean.ListUtils;
import com.blood.modules.system.dto.req.SysMenuPageReq;
import com.blood.modules.system.dto.req.SysMenuReq;
import com.blood.modules.system.dto.req.SysMenuSaveReq;
import com.blood.modules.system.dto.resp.SysMenuInfoResp;
import com.blood.modules.system.dto.resp.SysMenuPageResp;
import com.blood.modules.system.mapper.SysMenuMapper;
import com.blood.modules.system.model.SysMenu;
import com.blood.modules.system.service.ISysMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;


/**
 * 菜单权限Service业务层处理
 *
 * @author daoren
 * @date 2022-11-23 10:34:28
 */
@Service
public class SysMenuServiceImpl extends ICommonServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {
    @Autowired
    private Snowflake snowFlake;

    @Override
    public ResponseResult<PageResult<SysMenuPageResp>> pageList(SysMenuPageReq req) {
        PageResult<SysMenuPageResp> pageResult = PageResult.create(req.getPageNo(), req.getPageSize());
        LambdaQueryWrapper<SysMenu> qw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(req.getName())) {
            qw.like(SysMenu::getName, req.getName());
        }
        if (null != req.getType()) {
            qw.eq(SysMenu::getType, req.getType());
        }
        qw.orderByDesc(SysMenu::getCreateTime);

        IPage<SysMenu> iPage = baseMapper.selectPage(new Page<>(req.getPageNo(), req.getPageSize()), qw);
        if (iPage.getRecords().size() > 0) {
            List<SysMenuPageResp> queryRespList = ListUtils.copyProperties(iPage.getRecords(), SysMenuPageResp.class);
            return GlobalResultStatus.SUCCESS.toResult().response(pageResult.data(iPage.getTotal(), queryRespList));
        }
        return GlobalResultStatus.SUCCESS.toResult().response(pageResult.data(0, Collections.emptyList()));
    }

    @Override
    public List<SysMenu> queryList(SysMenuReq req) {
        LambdaQueryWrapper<SysMenu> lambda = new QueryWrapper<SysMenu>().lambda();
        if (null != req.getType()) {
            lambda.eq(SysMenu::getType, 0);
        }
        return baseMapper.selectList(lambda.eq(SysMenu::isDeleteFlag, false)
                .orderByAsc(SysMenu::getSort));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<Void> saveInfo(SysMenuSaveReq req) {
        String id = snowFlake.nextIdStr();
        SysMenu saveInfo = new SysMenu();
        BeanUtil.copyProperties(req, saveInfo);
        saveInfo.setId(id);
        baseMapper.insert(saveInfo);
        return GlobalResultStatus.SUCCESS.toResult().responseEmptyBody();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<Void> updateInfo(SysMenuSaveReq req) {
        SysMenu queryInfo = super.findById(req.getId());
        ObjectAssertUtil.objectIsNull(queryInfo, "未获取到详细信息!");
        BeanUtil.copyProperties(req, queryInfo);
        baseMapper.updateById(queryInfo);
        return GlobalResultStatus.SUCCESS.toResult().responseEmptyBody();
    }

    @Override
    public ResponseResult<SysMenuInfoResp> findDetailById(String id) {
        SysMenu queryInfo = super.findById(id);
        ObjectAssertUtil.objectIsNull(queryInfo, "详细信息不存在!");
        return GlobalResultStatus.SUCCESS.toResult().response(BeanUtil.copyProperties(queryInfo, SysMenuInfoResp.class));
    }
}
